package com.example.jacky.uilibrary.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by jacky on 2016/10/9.
 * sounbus app-developer
 */
public class ToggleButton extends View {
    /**
     * 开关状态
     */
    private ToggleState toggleState = ToggleState.open;
    /**
     * 背景图片
     */
    private Bitmap switchBitmap;
    /**
     * 滑动图片
     */
    private Bitmap slideBitmap;
    /**
     * 当前触摸点x坐标
     */
    private int currentX;
    /**
     * 手指是否在滑动
     */
    private boolean isSliding = false;
    public enum ToggleState{
        open,close
    }
    public ToggleButton(Context context) {
        super(context);
    }

    public ToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        //initView(switchBackground, slideButton);
    }

    /**
     * 设置开关背景
     * @param switchBackground
     */
    public void setSwitchBackgroundResource(int switchBackground) {
        switchBitmap = BitmapFactory.decodeResource(getResources(), switchBackground);
    }

    /**
     * 设置滑动按钮的图片
     * @param slideButton
     */
    public void setSlideButtonResource(int slideButton) {
        slideBitmap = BitmapFactory.decodeResource(getResources(), slideButton);
    }

    /**
     * 设置当前控件宽高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(switchBitmap.getWidth(), switchBitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1.绘制背景图片 left:图片左边坐标  top：图片顶部坐标
        canvas.drawBitmap(switchBitmap, 0, 0, null);
        //2.绘制滑动块的图片
        if(isSliding) {
            //手指在滑动
            int left = currentX - slideBitmap.getWidth()/2;
            if(left < 0) left = 0;
            if(left > switchBitmap.getWidth() -slideBitmap.getWidth())
                left = switchBitmap.getWidth() -slideBitmap.getWidth();

            canvas.drawBitmap(slideBitmap, left, 0, null);
        }else {
            //手指停止滑动
            if(toggleState == ToggleState.close) {
                canvas.drawBitmap(slideBitmap, 0, 0, null);
            }else {
                canvas.drawBitmap(slideBitmap, switchBitmap.getWidth() -slideBitmap.getWidth(), 0, null);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        currentX = (int) event.getX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isSliding = true;
                break;

            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_UP:
                isSliding = false;
                int centerX = switchBitmap.getWidth()/2;
                if(currentX <= centerX) {
                    //close,触摸点在左边
                    if(toggleState != ToggleState.close) {
                        toggleState = ToggleState.close;
                        if(listener != null) {
                            listener.onToggleStateChanged(toggleState);
                        }
                    }
                }else {
                    //open,触摸点在右边
                    if(toggleState != ToggleState.open) {
                        toggleState = ToggleState.open;
                        if(listener != null) {
                            listener.onToggleStateChanged(toggleState);
                        }
                    }
                }

                break;
        }
        invalidate();//调用onDraw方法，重绘改变滑动块位置
        return true;
    }
    /**
     * 设置开关状态
     * @param state
     */
    public void setToggleState(ToggleState state) {
        toggleState = state;
    }

    private OnToggleStateChangeListener listener;
    public interface OnToggleStateChangeListener {
        public void onToggleStateChanged (ToggleState state);
    }
    /**
     * 设置开关状态监听器
     * @param listener
     */
    public void setOnToggleStateChangeListener(OnToggleStateChangeListener listener) {
        this.listener = listener;
    }
}
